package com.aaa.cars.controller;

import com.aaa.cars.entity.CarType;
import com.aaa.cars.service.CarTypeService;
import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (CarType)表控制层
 *
 * @author makejava
 * @since 2020-11-17 19:19:56
 */
@RestController
@RequestMapping("carType")
public class CarTypeController {
    /**
     * 服务对象
     */
    @Resource
    private CarTypeService carTypeService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Result selectOne(Integer id) {
        //当ID为空时抛出自定义异常
        if (null==id){
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),this.carTypeService.queryById(id));
    }
    /**
     * 全部新增
     * @param carType
     * @return
     */
    @PostMapping("insertCarType")
    public Result insertCarType(@RequestBody CarType carType){
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),carTypeService.insert(carType));
    }

    /**
     * 修改数据
     * @param carType
     * @return
     */
    @PutMapping("updateCarType")
    public Result updateCarType(@RequestBody CarType carType){
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),carTypeService.update(carType));
    }

    /**
     * 根据ID删除
     * @param id
     * @return
     */
    @DeleteMapping("deleteCarType")
    public Result deleteCarType(Integer id){
        //当ID为空时抛出自定义异常
        if (null==id){
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),carTypeService.deleteById(id));
    }

    /**
     * 分页与模糊检索 车辆类型
     * @param pageNo
     * @param pageSize
     * @param carType
     * @return
     */
    @GetMapping("queryAllByCarType")
    public Result queryAllByCarType(Integer pageNo, Integer pageSize, CarType carType){
        return carTypeService.queryAllByCarType(pageNo,pageSize,carType);
    }



}